
import { specslist, specscount } from '../../../request/api'
const state = {
    specsList: [],
    size: 3,//查询条数
    page: 1,//页数
    total: 0//总条数

}

const getters = {
    getSpecs(state) {
        return state.specsList
    },
    getSize(state) {
        return state.size
    },
    getTotal(state) {
        return state.total
    }
}

const mutations = {
    CHANGE(state, payload) {
        state.specsList = payload
    },
    CHANGECOUNT(state, payload) {
        state.total = payload
    }
}

const actions = {
    GetSpecsInfo(context) {
        console.log(context);
        specslist({
            page: context.state.page,
            size: context.state.size
        })
            .then(res => {
                /*
                删除时 当删除的是非第一页的数据的最后一项时会出现一个bug,删除完成后他还是请求的是当前页,显示无数据应该是页码-1
                */
                if (res.code == 200) {
                    let list = res.list ? res.list : []
                    if (context.state.page != 1 && list.length == 0) {//重新调用接口渲染
                        context.dispatch('ChangePage', context.state.page - 1)
                    } else {
                        context.commit('CHANGE', res.list)
                    }
                }
            })
    },
    // 总条数
    GetCountInfo(context) {
        specscount()
            .then(res => {
                if (res.code == 200) {
                    console.log(res);
                    context.commit('CHANGECOUNT', res.list[0].total)
                }
            })
    },
    // 切换页码，重新调用接口数据
    ChangePage(context, payload) {
        context.state.page = payload
        context.dispatch('GetSpecsInfo', payload)
    }
}

export default {
    state,
    getters,
    mutations,
    actions,
    namespaced: true
}